package models.venue.seat;

import jodd.bean.BeanCopy;
import models.AdminSupplier;
import models.BaseModel;
import models.constants.DeletedStatus;
import models.merchant.Merchant;
import models.venue.enums.SeatClassify;
import play.modules.paginate.JPAExtPaginator;

import javax.persistence.*;
import java.util.Map;

/**
 * 座位
 * Created by hanbin on 17/4/6.
 */
@Entity
@Table(name="seat_infos")
public class Seat extends BaseModel {

    /**
     * 座位编号
     */
    @Column(name="seat_no", length = 255)
    public String seatNo;


    /**
     * 供应商
     */
    @ManyToOne
    @JoinColumn(name = "admin_supplier_id")
    public AdminSupplier adminSupplier;


    /**
     * 所属店面
     */
    @JoinColumn(name="merchant_id")
    @ManyToOne
    public Merchant merchant;

    @Enumerated(EnumType.STRING)
    @Column(name="seat_classify")
    public SeatClassify seatClassify;

    /**
     * 备注
     */
    @Column(name="remark", length = 255)
    public String remark;

    /**
     * 逻辑删除,0:未删除，1:已删除
     */
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted;

    /**
     * 分页查询.
     */
    public static JPAExtPaginator<Seat> findByCondition(Map<String, Object> conditionMap, String orderByExpress, int pageNumber, int pageSize) {
        StringBuilder xsqlBuilder = new StringBuilder("t.deleted=models.constants.DeletedStatus.UN_DELETED ")
                .append("/~ and t.seatNo = {seatNo} ~/")
                .append("/~ and t.seatClassify = {seatClassify} ~/")
                .append("/~ and t.adminSupplier.id = {supplierId} ~/")
                .append("/~ and t.merchant.id = {merchantId} ~/");

        //if(conditionMap.get("payedOrder")!=null)xsqlBuilder.append(" and (t.status ="+OrderStatus.PAID+" or t.status="+OrderStatus.DISTRIBUTION+") ");

        /**
         *    Query query = t.id = {id};
         *    query.setPropery("id" , conditionMap.get("id"));
         */
        util.xsql.XsqlBuilder.XsqlFilterResult result = new util.xsql.XsqlBuilder().generateHql(xsqlBuilder.toString(), conditionMap);
        JPAExtPaginator<Seat> orderPage = new JPAExtPaginator<Seat>("Seat t", "t", Seat.class,
                result.getXsql(), conditionMap).orderBy(orderByExpress);
        orderPage.setPageNumber(pageNumber);
        orderPage.setPageSize(pageSize);
        orderPage.setBoundaryControlsEnabled(false);
        return orderPage;
    }

    /**
     * Update
     * @param id
     * @param newObject
     */
    public static void update(Long id, Seat newObject) {
        Seat seat = Seat.findById(id);
        BeanCopy.beans(newObject, seat).ignoreNulls(true).copy();
        seat.save();
    }

    /**
     * 根据Id查询Seat
     * @param id
     * @return
     */
    public static Seat findBySeatId(long id){
        return Seat.find("id = ? and deleted = ?" , id , DeletedStatus.UN_DELETED).first();
    }

}
